import { Scene } from 'phaser'

export class Boot extends Scene {
  platforms
  player
  stars
  cursors
  score = 0
  scoreText
  bombs
  gameOver = false
  constructor() {
    super('Boot')
  }

  preload() {
    this.load.image('sky', 'assets/sky.png')
    this.load.image('ground', 'assets/platform.png')
    this.load.image('star', 'assets/star.png')
    this.load.image('bomb', 'assets/bomb.png')
    this.load.spritesheet('dude', 'assets/dude.png', { frameWidth: 32, frameHeight: 48 })
  }

  restartGame() {
    this.scene.restart()
  }

  create() {
    // 添加背景
    this.add.image(400, 300, 'sky')
    // 添加文字
    this.scoreText = this.add.text(16, 16, `得分: ${this.score}`, {
      fontSize: '32px',
      fill: '#000'
    })
    const restartButton = this.add
      .text(700, 16, '重新开始', { fontSize: '24px', fill: '#fff' })
      .setInteractive()
      .on('pointerdown', () => this.restartGame())

    //地面
    this.platforms = this.physics.add.staticGroup()

    this.platforms.create(400, 568, 'ground').setScale(2).refreshBody()

    this.platforms.create(600, 400, 'ground')
    this.platforms.create(50, 250, 'ground')
    this.platforms.create(750, 220, 'ground')
    //人物
    this.player = this.physics.add.sprite(100, 450, 'dude')

    this.player.setBounce(0.2)
    this.player.setCollideWorldBounds(true)

    this.anims.create({
      key: 'left',
      frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }),
      frameRate: 10,
      repeat: -1
    })

    this.anims.create({
      key: 'turn',
      frames: [{ key: 'dude', frame: 4 }],
      frameRate: 20
    })

    this.anims.create({
      key: 'right',
      frames: this.anims.generateFrameNumbers('dude', { start: 5, end: 8 }),
      frameRate: 10,
      repeat: -1
    })
    this.cursors = this.input.keyboard.createCursorKeys()
    // 星星
    this.stars = this.physics.add.group({
      key: 'star',
      repeat: 11,
      setXY: { x: 12, y: 0, stepX: 70 }
    })

    this.stars.children.iterate(function (child) {
      child.displayWidth = 20 // 设置宽度为 50 像素
      child.displayHeight = 20 // 设置高度为 50 像素

      child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8))
    })
    // 坏人
    this.bombs = this.physics.add.group()
    this.physics.add.collider(this.bombs, this.platforms)
    this.physics.add.collider(this.player, this.platforms)
    this.physics.add.collider(this.stars, this.platforms)

    this.physics.add.collider(this.player, this.bombs, this.hitBomb, null, this)

    this.physics.add.overlap(this.player, this.stars, this.collectStar, null, this)
  }
  collectStar(player, star) {
    star.disableBody(true, true)
    this.score += 10
    this.scoreText.setText(`得分: ${this.score}`)

    if (this.stars.countActive(true) === 0) {
      //  A new batch of stars to collect
      this.stars.children.iterate(function (child) {
        child.enableBody(true, child.x, 0, true, true)
      })

      var x = player.x < 400 ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400)

      var bomb = this.bombs.create(x, 16, 'bomb')
      bomb.setBounce(1)
      bomb.setCollideWorldBounds(true)
      bomb.setVelocity(Phaser.Math.Between(-200, 200), 20)
      bomb.allowGravity = false
    }
  }
  hitBomb() {
    console.log(1111)
    this.physics.pause()

    this.player.setTint(0xff0000)

    this.player.anims.play('turn')

    this.gameOver = true
  }
  update() {
    if (this.gameOver) return

    if (this.cursors.left.isDown) {
      this.player.setVelocityX(-160)

      this.player.anims.play('left', true)
    } else if (this.cursors.right.isDown) {
      this.player.setVelocityX(160)

      this.player.anims.play('right', true)
    } else {
      this.player.setVelocityX(0)

      this.player.anims.play('turn')
    }

    if (this.cursors.up.isDown && this.player.body.touching.down) {
      this.player.setVelocityY(-330)
    }
  }
}

